package cn.wolfcode.wolf2w.mgrsite.job;

import cn.wolfcode.wolf2w.domain.UserFavors;
import cn.wolfcode.wolf2w.redis.service.IStatisticsVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserFavorRedisService;
import cn.wolfcode.wolf2w.redis.vo.StatisticsVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserFavorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class RedisDataEnduranceJob {
    @Autowired
    private IStatisticsVORedisService statisticsVORedisService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserFavorRedisService userFavorRedisService;
    @Autowired
    private IUserFavorsService userFavorsService;
    @Scheduled(cron = "0/20 * * * * ?")
    public void enduranceStatisticsVOData(){
        List<StatisticsVO> vos = statisticsVORedisService.getAllStatisticsVO("*");
        if (vos.size() > 0) {
            for (StatisticsVO vo : vos) {
                strategyService.updateStatisticsVO(vo);
            }
        }
        System.out.println("+++++++++++++ddd++++++++++++");
    }

    @Scheduled(cron = "0/20 * * * * ?")
    public void enduranceUserFavorData(){
       List<String> idList = userFavorRedisService.getUserId("*");
        if (idList.size() > 0) {
            for (String uid : idList) {
                // 根据uid获取value并且保存到数据库中
                List<String> favorSid = userFavorRedisService.getFavorList(uid);
                UserFavors favors = userFavorsService.findByUid(uid);
                UserFavors userFavors = new UserFavors();
                if (favors == null) {
                    userFavors.setUid(uid);
                    userFavors.setFavorSid(favorSid);
                    userFavorsService.save(userFavors);
                }else {
                    favors.setUid(uid);
                    favors.setFavorSid(favorSid);
                    userFavorsService.update(favors);
                }
            }
        }
    }
}
